/**
 * @file  人员选择
 * @author hj
 */
import Vue from 'vue';
import resource from 'vue-resource';
var nativeApi = require('nativeApi');
import {APIS} from '../../js/config';

Vue.use(resource);

export default {
  state: {
    deptList: [], //  部门列表显示结果(包括搜索结果)
    deptData: [], //  部门初始数据(包括搜索结果)
    selectDept: '',
    personList: [], //  人员列表
    personData: [],  //  人员初始数据
    personSelectList: [],
    searchList: [],
    hWidth: {
      'width': 10
    },
    deptSearchVal: '',  //  部门关键字搜索
    personSearchVal: '',  //  人员关键字搜索
    isRefresh: true,  //  是否需要重新填充已被选择的人员
    isRadio: false //  是否单选
  },
  /* 部门列表 */
  initDeptEmp: function () {
    var self = this;
    nativeApi.initAjax({
      'type': 'get',
      'url': APIS.dept_list_all,
      callback: function (result) {
        if (result && result.code === 200) {
          var lists = result.data;
          for (var i = 0; i < lists.length; i++) {
            lists[i].selected = false;
            lists[i].deptId = lists[i].id;
          }
          self.state.deptData = lists;
          self.state.deptList = lists;
        } else {
          mui.alert(result.msg || '数据获取失败！', '提示', function () {
          });
        }
      }
    });
  },
  /* 搜索 */
  search: function (searchName, type) {
    var self = this;
    var data = type === 'dept' ? self.state.deptData : self.state.personData; //  数据源
    var matchData = []; //  匹配到的数据

    for (let i = 0; i < data.length; i++) {
      var item = data[i];
      if (item.name.indexOf(searchName) > -1) {
        var obj = JSON.parse(JSON.stringify(item));
        if (type === 'staff') {
          obj.selected = self.state.personSelectList.length > 0 && self.state.personSelectList[0].id === item.id;
        }
        matchData.push(obj);
      }
    }
    if (type === 'dept') {
      self.state.deptList = matchData;
    } else {
      self.state.personList = matchData;
    }
  },
  /* 查找部门下的人员 */
  deptEmp: function (deptId, cb) {
    var self = this;
    nativeApi.initAjax({
      'type': 'get',
      'url': APIS.dept_emp_list.replace('{id}', deptId),
      callback: function (result) {
        if (result && result.code === 200) {
          self.state.selectDept = deptId;
          var lists = result.data;
          if (lists.length === 0) {
            mui.alert('该部门没有员工！', '提示');
            return;
          }
          for (var i = 0; i < lists.length; i++) {
            lists[i].deptId = deptId;
            if (self.repetition(self.state.personSelectList, lists[i].id)) {
              lists[i].selected = true;
            } else {
              lists[i].selected = false;
            }
          }
          self.state.personSearchVal = '';
          self.state.personData = lists;
          self.state.personList = lists;
          var width = document.body.offsetWidth - 100,
            nowWid = self.state.personSelectList.length * 50;
          if (width < nowWid) {
            self.state.hWidth.width = width;
          } else {
            if (nowWid < 10) {
              nowWid = 10;
            }
            self.state.hWidth.width = nowWid;
          }
          if (typeof cb === 'function') {
            cb();
          }
        } else {
          mui.alert(result.msg || '数据获取失败！', '提示', function () {
          });
        }
      }
    });
  },
  /* 选中部门 */
  addDeptEmp: function (deptId, cb) {
    var self = this;
    nativeApi.initAjax({
      'type': 'get',
      'url': APIS.dept_emp_list.replace('{id}', deptId),
      callback: function (result) {
        if (result && result.code === 200) {
          var lists = result.data,
            selectLists = [];
          for (var i = 0; i < lists.length; i++) {
            lists[i].selected = true;
            lists[i].deptId = deptId;
            if (!self.repetition(self.state.personSelectList, lists[i].id)) {
              self.state.personSelectList.push(lists[i]);
            }
          }
          var width = document.body.offsetWidth - 100,
            nowWid = self.state.personSelectList.length * 50;
          if (width < nowWid) {
            self.state.hWidth.width = width;
          } else {
            if (nowWid < 10) {
              nowWid = 10;
            }
            self.state.hWidth.width = nowWid;
          }
          if (typeof cb === 'function') {
            cb();
          }
        } else {
          mui.alert(result.msg || '数据获取失败！', '提示', function () {
          });
        }
      }
    });
  },
  /* 选中人员 */
  addEmp: function (data) {
    var self = this,
      lists = self.state.personList,
      selectList = self.state.personSelectList,
      flag = false;
    for (var i = 0; i < lists.length; i++) {
      var id = lists[i].id || lists[i].ID;
      if (id === data.id) {
        lists[i].selected = true;
      }
    }
    for (var i = 0; i < selectList.length; i++) {
      if (selectList[i].id === data.id) {
        flag = true;
      }
    }
    if (!flag) {
      self.state.personSelectList.push(data);
      var width = document.body.offsetWidth - 100;
      var nowWid = self.state.personSelectList.length * 50;
      if (width < nowWid) {
        self.state.hWidth.width = width;
      } else {
        if (nowWid < 10) {
          nowWid = 10;
        }
        self.state.hWidth.width = nowWid;
      }
    }
  },
  /* 取消选中人员 */
  deleteEmp: function (id) {
    var self = this;
    var lists = self.state.personSelectList;
    var arr = [];
    for (var i = 0; i < lists.length; i++) {
      var sId = lists[i].ID || lists[i].id;
      if (sId != id) {
        arr.push(lists[i]);
      }
    }
    self.state.personSelectList = arr;
    var width = document.body.offsetWidth - 100;
    var nowWid = self.state.personSelectList.length * 50;
    if (width < nowWid) {
      self.state.hWidth.width = width;
    } else {
      if (nowWid < 10) {
        nowWid = 10;
      }
      self.state.hWidth.width = nowWid;
    }
    setTimeout(function () {
      self.updateSelectDept();
    }, 100);
  },
  /* 判断部门是否选中 */
  updateSelectDept: function () {
    /* 如果部门内所有成员没有全部选中，则该部门不选中，反之则选中 */
    var self = this;
    // var empList = self.state.personList;
    var empList = self.state.personData;
    var flag = false;
    var deptList = self.state.deptList;
    for (var i = 0; i < empList.length; i++) {
      if (!empList[i].selected) {
        flag = true;
      }
    }
    if (flag) {
      /* 没有全部选中 */
      for (var i = 0; i < deptList.length; i++) {
        if (deptList[i].deptId === self.state.selectDept) {
          deptList[i].selected = false;
        }
      }
    } else {
      /* 全部选中 */
      for (var i = 0; i < deptList.length; i++) {
        if (deptList[i].deptId === self.state.selectDept) {
          deptList[i].selected = true;
        }
      }
    }
  },
  setDeptSelect: function (deptId) {
    var lists = this.state.deptList;
    for (var i = 0; i < lists.length; i++) {
      if (lists[i].id === deptId) {
        lists[i].selected = true;
      }
    }
  },
  repetition: function (lists, id) {
    lists = lists || [];
    var flag = false;
    for (var i = 0; i < lists.length; i++) {
      var sId = lists[i].ID || lists[i].id;
      if (sId === id) {
        flag = true;
      }
    }
    return flag;
  },
  /* 取消选中部门 */
  deleteDeptEmp: function (deptId, cb) {
    var self = this;
    var lists = self.state.personSelectList;
    var arr = [];
    for (var i = 0; i < lists.length; i++) {
      if (lists[i].deptId != deptId) {
        arr.push(lists[i]);
      }
    }
    self.state.personSelectList = arr;
    var width = document.body.offsetWidth - 100;
    var nowWid = self.state.personSelectList.length * 50;
    if (width < nowWid) {
      self.state.hWidth.width = width;
    } else {
      if (nowWid < 10) {
        nowWid = 10;
      }
      self.state.hWidth.width = nowWid;
    }
    if (typeof cb === 'function') {
      cb();
    }
  },
  initSetDeptEmp: function (initResult) {
    var self = this;
    self.state.personSelectList = initResult.emp || [];
    var width = document.body.offsetWidth - 100;
    var nowWid = self.state.personSelectList.length * 50;
    if (width < nowWid) {
      self.state.hWidth.width = width;
    } else {
      if (nowWid < 10) {
        nowWid = 10;
      }
      self.state.hWidth.width = nowWid;
    }
    var deptList = this.state.deptList || [];
    var deptSelectList = initResult.dept || [];
    for (var i = 0; i < deptList.length; i++) {
      var flag = false;
      for (var j = 0; j < deptSelectList.length; j++) {
        if (deptList[i].deptId === deptSelectList[j].deptId) {
          flag = true;
        }
      }
      deptList[i].selected = flag;
    }
  }
};
